<?php
/**
 * Created by PhpStorm.
 * User: jurentech
 * Date: 2019-10-08
 * Time: 11:19
 */

namespace app\master\controller;

use tree\Data;

class Goods extends Base
{
    public function editgoods()
    {
        if (request()->isPost()) {
            $goods_id=input('goods_id');
            $data = input('post.');
            $goods = $data['goods'];
            $goods['good_img'] = implode(",", $data['good_img']);
            $goods['update_time'] = time();
//            $code = db('goods')->count() + 1;

//            $code = str_pad($code, 6, '0', STR_PAD_LEFT);
//            $goods['goods_sn'] = 'SLGJ' . $code;
//            $result = db('goods')->where(['goods_name' => $goods['goods_name'], 'isdelete' => 0])->count();
//            if ($result > 0) {
//                return $this->_ajax_return(602, '商品已存在');
//            }
            db('goods')->where(['goods_id'=>$goods_id])->update($goods);
            $guige = [];
            db('goods_type_price')->where(['goods_id'=>$goods_id])->delete();
            foreach ($data['key_name'] as $k => $v) {
                $guige = [
                    "key_name" => $data['key_name'][$k],
                    "price" => $data['price'][$k],
                    "store_count" => $data['store_count'][$k],
                    "spec_img" => $data['spec_img'][$k],
                    "goods_id"=>$goods_id

                ];

                $result=db('goods_type_price')->insertGetId($guige);
            }


            $commiss = [
                "invit" => $data['invit'],
                "buy" => $data['buy'],
                "goods_id" => $goods_id

            ];
            db('goods_commiss')->where(['goods_id'=>$goods_id])->delete();
            db('goods_commiss')->insert($commiss);

            return $this->_ajax_return(200,'修改成功','goods',$data);

        }
        $id=input('id');
        $good['goods']=db('goods')->where(['goods_id'=>$id])->find();
        $good['goods']['good_img']=explode(",", $good['goods']['good_img']);;
        $good['goods_type_price']=db('goods_type_price')->where(['goods_id'=>$id])->select();
        $good['goods_commiss']=db('goods_commiss')->where(['goods_id'=>$id])->find();
        $data = db('goods_category')->where(['isdelete' => 0])->select();
        $brand = db('brand')->select();
//        var_dump($good);
        $this->assign('brand', $brand);
        $this->assign('goods',$good);
        $this->assign('category', $data);
        return $this->fetch();
    }
    public function addgoods()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $goods = $data['goods'];
            $goods['good_img'] = implode(",", $data['good_img']);
            $goods['create_time'] = time();
            $code = db('goods')->count() + 1;

            $code = str_pad($code, 6, '0', STR_PAD_LEFT);
            $goods['goods_sn'] = 'SLGJ' . $code;
            $result = db('goods')->where(['goods_name' => $goods['goods_name'], 'isdelete' => 0])->count();
            if ($result > 0) {
                return $this->_ajax_return(602, '商品已存在');
            }
            $gooods_id = db('goods')->insertGetId($goods);
            $guige = [];
            foreach ($data['key_name'] as $k => $v) {
                $guige = [
                    "key_name" => $data['key_name'][$k],
                    "price" => $data['price'][$k],
                    "store_count" => $data['store_count'][$k],
                    "spec_img" => $data['spec_img'][$k],
                    "goods_id"=>$gooods_id
                ];
                db('goods_type_price')->insert($guige);
            }


            $commiss = [
                "invit" => $data['invit'],
                "buy" => $data['buy'],
                "goods_id" => $gooods_id

            ];
            db('goods_commiss')->insert($commiss);

            return $this->_ajax_return(200,'添加成功','goods',$data);

        }
        $data = db('goods_category')->where(['isdelete' => 0])->select();
        $brand = db('brand')->select();
        $this->assign('brand', $brand);
        $this->assign('category', $data);
        return $this->fetch();
    }

    public function getflist()
    {
        $data = input('post.pid');
        $data = db('goods_category')->where(['isdelete' => 0, 'parent_id' => $data])->select();


        return $this->_ajax_return(200, '成功', '', $data);
    }

    public function goods()
    {
        $data['goods_name'] = input('post.goods_name');
        $data['virtual_sales_sum1'] = input('post.virtual_sales_sum1');
        $data['virtual_sales_sum2'] = input('post.virtual_sales_sum2');
        $data['shop_price1'] = input('post.shop_price1');
        $data['shop_price2'] = input('post.shop_price2');
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function goodslist()
    {
//        array('between','1,8');
        $data['goods_name'] = input('goods_name');
        $data['virtual_sales_sum1'] = input('virtual_sales_sum1');
        $data['virtual_sales_sum2'] = input('virtual_sales_sum2');
        $data['shop_price1'] = input('shop_price1');
        $data['shop_price2'] = input('shop_price2');
        $where['isdelete'] = 0;
        if ($data['goods_name']) {
            $where['goods_name'] = ['like', "%" . $data['goods_name'] . "%"];
        }
        if (!$data['virtual_sales_sum1']) {
            $data['virtual_sales_sum1'] = 0;
        }
        if (!$data['virtual_sales_sum2']) {
            $data['virtual_sales_sum2'] = 9999;
        }
        if (!$data['shop_price1']) {
            $data['shop_price1'] = 0;
        }
        if (!$data['shop_price2']) {
            $data['shop_price2'] = 99999;
        }
        $where['virtual_sales_sum'] = ['between', $data['virtual_sales_sum1'] . ',' . $data['virtual_sales_sum2']];
        $where['shop_price'] = ['between', $data['shop_price1'] . ',' . $data['shop_price2']];
//        var_dump($where);
        $page['page'] = input("page");
        $page['limit'] = input("limit");
        $result['data'] = db('goods')
            ->where($where)
            ->order('sort DESC')
            ->page($page['page'], $page['limit'])
            ->select();
        foreach ($result['data'] as $k => $v) {
            $result['data'][$k]['good_img'] = explode(",", $result['data'][$k]['good_img']);
            $result['data'][$k]['create_time'] = date('Y-m-d H:i:s', $result['data'][$k]['create_time']);
            $result['data'][$k]['virtual_collect_sum'] = db('goods_visit')->where(['goods_id' => $result['data'][$k]['goods_id']])->count();
        }
        $result['count'] = db('goods')->where($where)->count();
        return $this->_table_return(0, "", $result['count'], $result['data']);
    }

    public function yunfei()
    {
        return $this->fetch();
    }

    public function addpinpai()
    {
        if (request()->isPost()) {
            $data = input('post.');
            unset($data['file']);
            $result = db('brand')->where(['name' => $data['name']])->find();
            if ($result) {
                return $this->_ajax_return(602, '已存在品牌，不能重复添加');
            }
            db('brand')->insert($data);
            return $this->_ajax_return(200, '添加成功', 'pinpai');
        }
        return $this->fetch();
    }

    public function pinpai()
    {

        return $this->fetch();
    }

    public function delectpenpai()
    {
        $id = input('id');
        $data = db('goods')->where(['brand_id' => $id, 'isdelete' => 0])->count();
        if ($data > 0) {
            return $this->_ajax_return(602, '该品牌下存在商品不可删除');
        }
        db('brand')->where(['id' => $id])->delete();
        return $this->_ajax_return(200, '删除成功');
    }

    public function getpinpai()
    {
        $page['page'] = input("page");
        $page['limit'] = input("limit");
        $result['data'] = db('brand')
            ->order('sort DESC')
            ->page($page['page'], $page['limit'])
            ->select();
        $result['count'] = db('brand')->count();
        return $this->_table_return(0, "", $result['count'], $result['data']);

    }

    public function editpinpai()
    {
        $field = input('post.field');
        $value = input('post.value');
        $id = input('post.id');
        db('brand')->where(['id' => $id])->update([$field => $value]);
        $this->_ajax_return(200, '修改成功');
    }

    public function delectfenlei()
    {
        $id = input('id');
        db('goods_category')->where(['id' => $id])->update(['isdelete' => 1]);
        return $this->_ajax_return(200, '删除成功');
    }

    public function addfenlei()
    {
        if (request()->isPost()) {
            $data = input('post.');
            if (!isset($data['status'])) {

                $data['status'] = 1;
            } else {
                $data['status'] = 0;
            }
            if (!isset($data['is_hot'])) {
                $data['is_hot'] = 1;
            } else {
                $data['is_hot'] = 0;
            }
            unset($data['file']);
            if($data['parent_id']==0){
                $data['level']=1;
            }else{
                $data['level']=2;
            }


            db('goods_category')->insert($data);
            return $this->_ajax_return(200, '添加成功', 'fenlei');
        }
        $data = db('goods_category')->where(['isdelete' => 0, 'parent_id' => 0])->select();
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function fenlei()
    {
        if (request()->isPost()) {
            $id = input('id');
            $type = input('type');
            $types = input('types');
            if ($types == 'true' && $type == '1') {
                $types = 0;
            } else if ($types == 'false' && $type == '1') {
                $types = 1;
            } else if ($types == 'false' && $type == '2') {
                $types = 0;
            } else if ($types == 'true' && $type == '2') {
                $types = 1;
            }
            if ($type == 1) {
                $type = 'is_hot';
            } else {
                $type = 'status';
            }

            $result = db('goods_category')->where(['id' => $id])->update([$type => $types]);

        }
        return $this->fetch();
    }

    public function secoundfenlei()
    {
        $id = input('get.id');
        $this->assign('id', $id);
        return $this->fetch();
    }

    public function getfenlei()
    {
        $page['page'] = input("page");
        $page['limit'] = input("limit");
        $result['data'] = db('goods_category')
            ->where(['isdelete' => 0, 'parent_id' => 0])
            ->order('sort_order DESC')
            ->page($page['page'], $page['limit'])
            ->select();
        $result['count'] = db('goods_category')->where(['isdelete' => 0, 'parent_id' => 0])->count();
        return $this->_table_return(0, "", $result['count'], $result['data']);

    }

    public function secondlist()
    {
        $id = input('id');
        $page['page'] = input("page");
        $page['limit'] = input("limit");
        $result['data'] = db('goods_category')
            ->where(['isdelete' => 0, 'parent_id' => $id])
            ->order('sort_order DESC')
            ->page($page['page'], $page['limit'])
            ->select();
        $result['count'] = db('goods_category')->where(['isdelete' => 0, 'parent_id' => $id])->count();
        return $this->_table_return(0, "", $result['count'], $result['data']);
    }

    public function editfenlei()
    {
        $field = input('post.field');
        $value = input('post.value');
        $id = input('post.id');
        db('goods_category')->where(['id' => $id])->update([$field => $value]);
        $this->_ajax_return(200, '修改成功');

    }


}